package com.zb.jnlxc.action;

import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;

import org.dom4j.DocumentException;
import org.jbpm.api.ProcessInstance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ZLHW.base.Exception.BaseErrorModel;
import com.zb.jnlxc.form.MiniPageReq;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.PaiChanRecord;
import com.zb.jnlxc.service.PaichanRecordService;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 12-12-29
 * Time: 下午3:32
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/ProductAction")
@SessionAttributes(value = {"user"})
public class ProductAction {
    @Resource
    PaichanRecordService paichanRecordService;

    @ResponseBody
    @RequestMapping("/getProductTrace")
    public MiniPageRsp getProductTrace(String taskId) {
        List result = paichanRecordService.getProductTrace(taskId);
        return new MiniPageRsp(result, result.size());
    }

    @RequestMapping("/downloadPDF")
    public void downloadPDF(HttpServletResponse response, String taskId) throws IOException {

        OutputStream out = response.getOutputStream();
        PaiChanRecord paiChanRecord = paichanRecordService.getPaiChanRecordByTaskId(taskId);
        response.setContentType("application/x-download");
        response.setHeader("Content-disposition", "attachment;filename=" + paiChanRecord.getCode()
                + ".pdf");
        try {
            paichanRecordService.startCreatePDF(taskId, out);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }

    @ResponseBody
    @RequestMapping("/loadProductRecord")
    public MiniPageRsp loadProductRecord(MiniPageReq page, HttpServletRequest request)
            throws ParseException {
        page.setRequest(request);
        paichanRecordService.loadProductRecordByPage(page);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }


    @ResponseBody
    @RequestMapping("/printForm")
    public void printForm(String taskId, String remarks, @ModelAttribute("user") Admin user)
            throws JRException,
            BaseErrorModel {
        paichanRecordService.printForm(taskId, remarks, user);
    }

    @ResponseBody
    @RequestMapping("/startCreatePDF")
    public void startCreatePDF(String taskId, OutputStream os) throws JRException,
            DocumentException {
        paichanRecordService.startCreatePDF(taskId, os);
    }


    @ResponseBody
    @RequestMapping("/delete")
    public void delete(PaiChanRecord paiChanRecord) {
        paichanRecordService.delete(paiChanRecord);
    }

    @ResponseBody
    @RequestMapping("/deleteProductFlowInstanceCascade")
    public void deleteProductFlowInstanceCascade(String id) {
        paichanRecordService.deleteProductFlowInstanceCascade(id);
    }

    @ResponseBody
    @RequestMapping("/getProductRecordInfo")
    public Map getProductRecordInfo(String taskId) {
        return paichanRecordService.getProductRecordInfo(taskId);
    }

    @ResponseBody
    @RequestMapping("/findProductRecordFlowInstanceByKey")
    public ProcessInstance findProductRecordFlowInstanceByKey(String id) {
        return paichanRecordService.findProductRecordFlowInstanceByKey(id);
    }
}
